/*
 * Copyright 2012 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.yangfei.SportAssistant.fragments;

import com.yangfei.SportAssistant.R;
import com.yangfei.SportAssistant.ChartActivity;
import com.yangfei.SportAssistant.ChartView;
import com.yangfei.SportAssistant.MainTabActivity;
import com.yangfei.SportAssistant.util.Constants;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;

/**
 * A DialogFragment to show chart settings.
 * 
 * @author Jimmy Shih
 */
public class ChartSettingsDialog extends Dialog
{

	public static final String CHART_SETTINGS_DIALOG_TAG = "chartSettingsDialog";
	Context mContext = null;
	private RadioGroup radioGroup;
	private CheckBox[] checkBoxes;
	
	public ChartSettingsDialog(Context context)
	{
		super(context);
		mContext = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.chart_settings);
		
		Button cancel = (Button) findViewById(R.id.chart_settings_cancel);
    cancel.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        dismiss();
      }
    });
    Button ok = (Button) findViewById(R.id.chart_settings_ok);
    ok.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        handleOk();
      }
    });
/*		new AlertDialog.Builder(mContext).setCancelable(true).setNegativeButton(R.string.generic_cancel, null).setPositiveButton(R.string.generic_ok, new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				chartFragment.setMode(radioGroup.getCheckedRadioButtonId() == R.id.chart_settings_by_distance ? ChartView.Mode.BY_DISTANCE
						: ChartView.Mode.BY_TIME);
				for (int i = 0; i < ChartView.NUM_SERIES; i++)
				{
					chartFragment.setChartValueSeriesEnabled(i, checkBoxes[i].isChecked());
				}
				chartFragment.update();
			}
		}).setTitle(R.string.menu_chart_settings).setView(view).create();*/
	}
	
  private void handleOk() {
  	
  	final ChartActivity chartFragment = (ChartActivity)getOwnerActivity();
  	
  	chartFragment.setMode(radioGroup.getCheckedRadioButtonId() == R.id.chart_settings_by_distance ? ChartView.Mode.BY_DISTANCE
				: ChartView.Mode.BY_TIME);
		for (int i = 0; i < ChartView.NUM_SERIES; i++)
		{
			chartFragment.setChartValueSeriesEnabled(i, checkBoxes[i].isChecked());
		}
		chartFragment.update();

    dismiss();
  }
	
	public void setup() 
	{
		final ChartActivity chartFragment = (ChartActivity) MainTabActivity.getInstance().getTabActivity(Constants.TAB_ITEM_CHART);

		radioGroup = (RadioGroup) findViewById(R.id.chart_settings_x);
		radioGroup.check(chartFragment.getMode() == ChartView.Mode.BY_DISTANCE ? R.id.chart_settings_by_distance
				: R.id.chart_settings_by_time);

		
		checkBoxes = new CheckBox[ChartView.NUM_SERIES];
		checkBoxes[ChartView.ELEVATION_SERIES] = (CheckBox) findViewById(R.id.chart_settings_elevation);
		checkBoxes[ChartView.SPEED_SERIES] = (CheckBox) findViewById(R.id.chart_settings_speed);
		checkBoxes[ChartView.POWER_SERIES] = (CheckBox) findViewById(R.id.chart_settings_power);
		checkBoxes[ChartView.CADENCE_SERIES] = (CheckBox) findViewById(R.id.chart_settings_cadence);
		checkBoxes[ChartView.HEART_RATE_SERIES] = (CheckBox) findViewById(R.id.chart_settings_heart_rate);

		// set checkboxes values
		for (int i = 0; i < ChartView.NUM_SERIES; i++)
		{
			checkBoxes[i].setChecked(chartFragment.isChartValueSeriesEnabled(i));
		}
		checkBoxes[ChartView.SPEED_SERIES].setText(chartFragment.isReportSpeed() ? R.string.stat_speed : R.string.stat_pace);

	}
}